Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

jest: Fix configuration of Webpack aliases #1577

Merged
merged 3 commits into from
Jun 25, 2020

Conversation

constgen
Copy link
Contributor

@constgen constgen commented May 21, 2020

If we configure alias to React in Webpack which is required by react-hot-loader the Jest tests fail with an error

Configuration error:

    Could not locate module react mapped as:
    C:\Projects\project\node_modules\react$1.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^react$/": "C:\Projects\project\node_modules\react$1"
      },
      "resolver": undefined
    }

    > 1 | import React from 'react';
        | ^
      2 | import PropTypes from 'prop-types';

To reproduce this we need to add something like that to the Webpack aliases configuration

neutrino.config.resolve.alias.set('react', path.resolve(path.join(projectNodeModulesPath, 'react')))

And import any JSX component in tests.

I am curios why we need this $1 at the end. It only makes sense if the key contains angle brackets () like:

Example from Jest docs

"module_name_(.*)": "<rootDir>/substituted_module_$1.js",
"assets/(.*)": [
      "<rootDir>/images/$1",
      "<rootDir>/photos/$1",
      "<rootDir>/recipes/$1"
]

But we know that we can't ever have angle brackets in the alias key.

The issue can be fixed by removing $1 placeholder

Fixes #1552.

Copy link
Member

@edmorley edmorley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot! This appears to have been a regression from #487 - where the (.*) group match was removed from the key, but the $1 not also removed from the value:
d3b35a5#diff-204df345f1bb711e711da06c51ea8896

Please could you add a test for this, to prevent further regressions? :-)

@constgen
Copy link
Contributor Author

constgen commented Jun 22, 2020

Please, review. New tests passed. But unfortunately the build job failed any way. This happens because of incompatibility with the latest ESlint v7 as I understand looking to logs
изображение

We really should not accept ESlint 7 in peer dependencies

@constgen constgen requested a review from edmorley June 24, 2020 16:04
@edmorley
Copy link
Member

We really should not accept ESlint 7 in peer dependencies

ESLint 7 works fine with Neutrino and the tests passed when #1572 was merged. The issue was that ESLint 7.3.0 introduced a breaking change/regression (eslint/eslint/issues/13427), which was now been fixed in 7.3.1. I've retriggered the tests and they now pass.

@edmorley edmorley changed the title Fix configuration of Webpack aliases jest: Fix configuration of Webpack aliases Jun 25, 2020
Copy link
Member

@edmorley edmorley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding the test :-)

@edmorley edmorley merged commit 3e66d22 into neutrinojs:master Jun 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

Preact + Jest: "Could not locate module react mapped as ... node_modules/preact/compat$1"
2 participants